package com.hu.zhcc.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.hu.zhcc.shiro.entity.bo.CheckGroup;
import com.hu.zhcc.shiro.entity.bo.CheckItem;
import com.hu.zhcc.shiro.entity.dto.UserDTO;
import com.hu.zhcc.shiro.entity.vo.CheckGroupVO;
import com.hu.zhcc.shiro.entity.vo.CheckItemVO;
import com.hu.zhcc.shiro.entity.vo.UserDetailVO;
import com.hu.zhcc.shiro.service.CheckGroupService;
import com.hu.zhcc.shiro.service.CheckItemService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查组管理
 */

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    @GetMapping("")
    public PageInfo<CheckGroup> homeCheckGroup(CheckGroupVO checkGroupVO){
        PageInfo<CheckGroup> checkGroupPageInfo = checkGroupService.pageQuery(checkGroupVO);
        return checkGroupPageInfo;
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeCheckGroup(@PathVariable("id") int id) {
        return checkGroupService.delete(id) > 0 ? ResponseEntity.noContent().build() :
                ResponseEntity.notFound().build();
    }

    @PostMapping("")
    public ResponseEntity<?> saveCheckGroup(@RequestBody CheckGroup checkGroup) {
        List<CheckItem> checkItems = checkGroup.getCheckItems();
        Integer[] checkitemIds=new Integer[checkItems.size()];
        for (int i=0;i<checkItems.size();i++) {
            checkitemIds[i] = checkItems.get(i).getId();
        }
        return checkGroupService.add(checkGroup,checkitemIds) > 0 ? ResponseEntity.noContent().build() :
                ResponseEntity.notFound().build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateCheckGroup(@PathVariable("id") int id, @RequestBody CheckGroup checkGroup) {
        List<CheckItem> checkItems = checkGroup.getCheckItems();
        Integer[] checkitemIds=new Integer[checkItems.size()];
        for (int i=0;i<checkItems.size();i++) {
            checkitemIds[i] = checkItems.get(i).getId();
        }
        int edit = checkGroupService.edit(checkGroup, checkitemIds);
        return edit == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(edit);
    }

    @GetMapping("/all")
    public List<CheckGroup> listAllCheckItem() {
        return checkGroupService.findAll();
    }

    @GetMapping("/{id}")
    public List<CheckGroup> getById(@PathVariable("id") int id) {
        return checkGroupService.selectBySetmealId(id);
    }

}
